﻿using System.Linq;
using System.Web.Mvc;
using UscSunShine.Models;


namespace UscSunShine.Mvc.Controllers
{
    public class StatisticsController : Controller
    {
        private ApplicationDbContext db = new ApplicationDbContext();

        /// <summary>
        /// GET: /Statistics/
        /// 只允许子操作调用
        /// </summary>
        /// <returns></returns>

        [ChildActionOnly]
        public  ActionResult ChildIndex()
        {
            //获取统计信息
            //按处理状态分组，统计申请数量
            var result = db.Requests.GroupBy(t => t.ProcessStatus)
                            .Select(s => new { Key = s.Key, Count = s.Count() })
                            .ToList();
            
            //按条件汇总申请数量
            var statistics = new Statistics
            {
                CountOfRequest = result.Sum(t => t.Count),
                CountOfProcessing = result.Where(t => UscSunShine.Models.Request.StatusProcessing.Contains(t.Key))
                                          .Sum(t => t.Count),
                CountOfProcessed = result.Where(t => UscSunShine.Models.Request.StatusProcessed.Contains(t.Key))
                                         .Sum(t => t.Count)
            };
            //返回分部视图   
            return PartialView(statistics);
        }

    }
}
